

#include	"SysConfig.h"
#include	"Define.h"
#include	"Tuner.h"
#include	"Model_Option.h"
#include	"Display.h"
#ifdef  ST7565P
#include	"ST7565P.h"
#endif
#ifdef  MIC
#include	"MIC.h"
#endif

extern UI_LOCAL ucUI_OPT_Local;

extern unsigned char	ucTuner_CurrentBand;
extern unsigned short	usTuner_Frequency_Current[UI_TUNER_BAND_NUM];	// 8700 ~ 10800
extern unsigned char	ucTuner_PresetNumber[UI_TUNER_BAND_NUM];
extern unsigned short	usTuner_PresetFrequency[UI_TUNER_BAND_NUM][UI_TUNER_PRESET_NUM];
extern unsigned char	ucTuner_Mode;
extern unsigned char	ucTuner_ModeSeq;

extern unsigned char ucDISP_Blink;
extern unsigned char ucDISP_Buf[12];
extern unsigned char ucTUNER_Stereo;

extern unsigned char TUNER_CurrentBandType(void);

unsigned char TUNER_DISP_GetBlinkFlag(void)
{
	if  (ucDISP_Blink == 0)
	{
		if	(ucTuner_Mode == TUNER_SCAN)
		{
			if  (ucTuner_ModeSeq == TUNER_SCAN_WAIT_5SEC)
			{
				return(1);
			}
		}
		if  (ucTuner_Mode == TUNER_AST)
		{
			if  (ucTuner_ModeSeq == TUNER_AST_DEMO_WAIT_5S)
			{
				return(1);
			}	
		}
	}	
	return(0);
}

void TUNER_DISP_IconMain(void)
{
	if  (ucTUNER_Stereo  == 1)
	{
		DISP_IconSetStatusFlag(1, ICON_MASK_ST);
	}	
}
                
void TUNER_DISP_PresetNumber(void)
{
	unsigned short usFreq = usTuner_Frequency_Current[ucTuner_CurrentBand];
	unsigned char ucNumber;

	if  (usFreq == usTuner_PresetFrequency[ucTuner_CurrentBand][ucTuner_PresetNumber[ucTuner_CurrentBand] - 1])
	{		
		ucNumber = ucTuner_PresetNumber[ucTuner_CurrentBand];//preset Number
	#ifdef  ST7565P
		if	(ucNumber == 1)
		{
			ST7565_UpdateDisplayImage(5, IMAGE_CH1);
		}
		if	(ucNumber == 2)
		{
			ST7565_UpdateDisplayImage(5, IMAGE_CH2);
		}
		if	(ucNumber == 3)
		{
			ST7565_UpdateDisplayImage(5, IMAGE_CH3);
		}
		if	(ucNumber == 4)
		{
			ST7565_UpdateDisplayImage(5, IMAGE_CH4);
		}
		if	(ucNumber == 5)
		{
			ST7565_UpdateDisplayImage(5, IMAGE_CH5);
		}
		if	(ucNumber == 6)
		{
			ST7565_UpdateDisplayImage(5, IMAGE_CH6);
		}
	#endif
	#ifdef  PT6524
		if  (ucNumber != 0)
		{
			ucDISP_Buf[3] = 'C';
			ucDISP_Buf[4] = 'H';
			ucDISP_Buf[5] = '0' + ucNumber;
		}
	#endif
	}	
}

void TUNER_DISP_Frequency(void)
{
	unsigned short usFreq = usTuner_Frequency_Current[ucTuner_CurrentBand];	
	unsigned char ucLocal = ucUI_OPT_Local;	
	unsigned char ucBuf[10];

	memset(&ucBuf[0], ' ', 10); 
	
#ifdef  ST7565P	
	ucBuf[0] = (usFreq / 10000) + '0';
	usFreq = usFreq % 10000;

	ucBuf[1] = (usFreq / 1000) + '0';
	usFreq = usFreq % 1000;	

	ucBuf[2] = (usFreq / 100) + '0';
	usFreq = usFreq % 100;

	if	((ucTuner_CurrentBand == FM1) || (ucTuner_CurrentBand == FM2) || (ucTuner_CurrentBand == FMA))
	{
		ucBuf[3] = '.'; 
		ucBuf[4] = (usFreq / 10) + '0';
		usFreq = usFreq % 10;
	
		if	(ucLocal == LOCAL_EUROPE)
		{
			ucBuf[5] = usFreq + '0';
		}
		else
		{
			ucBuf[5] = ' ';	
		}		
	}
	else
	{
		ucBuf[3] = (usFreq / 10) + '0';
		usFreq = usFreq % 10;	
		ucBuf[4] = usFreq + '0';
	}	
	
	switch  (ucTuner_CurrentBand)
	{
		case FM1:
		case FM2:
		case FMA:			
			if	(ucBuf[0] == '0')
			{
				ST7565_UpdateDisplayData(28, ucBuf[1]);	// 27
				ST7565_UpdateDisplayData(36, ucBuf[2]);	
				ST7565_UpdateDisplayData(43, ucBuf[3]);
				ST7565_UpdateDisplayData(46, ucBuf[4]);		
				ST7565_UpdateDisplayData(53, ucBuf[5]);	
	 		}
			else
			{
				ST7565_UpdateDisplayData(25, ucBuf[0]);
				ST7565_UpdateDisplayData(31, ucBuf[1]);	// 30
				ST7565_UpdateDisplayData(39, ucBuf[2]);	
				ST7565_UpdateDisplayData(46, ucBuf[3]);
				ST7565_UpdateDisplayData(49, ucBuf[4]);		
				ST7565_UpdateDisplayData(56, ucBuf[5]);
			}
			if	(ucLocal == LOCAL_EUROPE)
			{
				ST7565_UpdateDisplayImage(67, IMAGE_MHZ);								
			}
			else
			{	
				ST7565_UpdateDisplayImage(58, IMAGE_MHZ);
			}					
			break;
		case AM1:			
		case AM2:			
		case AMA:
			if	(ucBuf[1] == '0')
			{	
				ST7565_UpdateDisplayData(29, ucBuf[2]);	
				ST7565_UpdateDisplayData(38, ucBuf[3]);	
				ST7565_UpdateDisplayData(47, ucBuf[4]);									
			}
			else
			{
			 	ST7565_UpdateDisplayData(25, ucBuf[1]);	
			 	ST7565_UpdateDisplayData(32, ucBuf[2]);	
				ST7565_UpdateDisplayData(41, ucBuf[3]);	
				ST7565_UpdateDisplayData(50, ucBuf[4]);	
			}	
			ST7565_UpdateDisplayImage(58, IMAGE_KHZ);
			break;
		}		
#endif
#ifdef  PT6524
	ucDISP_Buf[6] = ' ';
		
	if  ((ucTuner_CurrentBand == FM1) || (ucTuner_CurrentBand == FM2)
	    || (ucTuner_CurrentBand == FMA))
	{
	
		ucDISP_Buf[7] = (usFreq / 10000) + '0';
		usFreq = usFreq % 10000;
		if	(ucDISP_Buf[7] == '0')
		{
			ucDISP_Buf[7] = ' ';
		}

		ucDISP_Buf[8] = (usFreq / 1000) + '0';
		usFreq = usFreq % 1000; 

		ucDISP_Buf[9] = (usFreq / 100) + '0';
		usFreq = usFreq % 100;

		ucDISP_Buf[10] = (usFreq / 10) + '0';
		usFreq = usFreq % 10;		

		if	(ucLocal == LOCAL_EUROPE)
		{
			ucDISP_Buf[11] = usFreq + '0';
		}
		DISP_IconSetStatusFlag(1, ICON_MASK_P);
	}
	else
	{
		ucDISP_Buf[7] = (usFreq / 1000) + '0';
		usFreq = usFreq % 1000;

		if	(ucDISP_Buf[7] == '0')
		{
			ucDISP_Buf[7] = ' ';
		}	
		ucDISP_Buf[8] = (usFreq / 100) + '0';
		usFreq = usFreq % 100;

		ucDISP_Buf[9] = (usFreq / 10) + '0';
		usFreq = usFreq % 10;
		ucDISP_Buf[10] = usFreq + '0';
		ucDISP_Buf[11] = ' ';
	}	
#endif
	
}

void TUNER_DISP_SubText(void)
{
	unsigned char ucBuf[3];
	
	memset(&ucBuf[0], ' ', 3);

	if  (ucTuner_Mode == TUNER_AST)
	{
		switch  (ucTuner_ModeSeq)
		{
			case TUNER_AST_SETMUTE:
			case TUNER_AST_SET_SEEK:
			case TUNER_AST_FREQ_UP:
			case TUNER_AST_SET_FREQ:
			case TUNER_AST_GET_SIGNAL:
			case TUNER_AST_SET_PRESET:
			case TUNER_AST_REL_SEEK:
				ucBuf[0] = 'A';
				ucBuf[1] = 'S';
				ucBuf[2] = 'T';
			#ifdef  ST7565P
				ST7565_UpdateSubDisplayData(&ucBuf[0]);
			#endif
			#ifdef  PT6524
				memcpy(&ucDISP_Buf[0], &ucBuf[0], 3);	
			#endif
				return;
		}
				
	}
	
	if	(ucTuner_Mode == TUNER_SCAN) 
	{
		switch  (ucTuner_ModeSeq)
		{
			case TUNER_SCAN_WAIT_5SEC:
			case TUNER_SCAN_WAIT_5SEC_RELMUTE:
				break;
			default:
			#ifdef  MODEL_ARA5040
				if  (ucDISP_Blink == 0)
				{
					ucBuf[0] = 'S';
					ucBuf[1] = 'C';
					ucBuf[2] = 'N';
				}
			#else
				ucBuf[0] = 'S';
				ucBuf[1] = 'C';
				ucBuf[2] = 'N';
			#endif
			#ifdef  ST7565P
				ST7565_UpdateSubDisplayData(&ucBuf[0]);
			#endif
			#ifdef  PT6524
				memcpy(&ucDISP_Buf[0], &ucBuf[0], 3);	
			#endif
				return;
		}
	}
	
	if  (TUNER_CurrentBandType() == FM)
	{
		ucBuf[0] = 'F';
	}
	else
	{		
		ucBuf[0] = 'A';
	}
	ucBuf[1] = 'M';
	switch (ucTuner_CurrentBand)
	{
		case FM1:
			ucBuf[2] = '1';
			break;
		case FM2:
			ucBuf[2] = '2';
			break;
		case FMA:
			ucBuf[2] = 'A';
			break;
		case AM1:
			ucBuf[2] = '1';
			break;
		case AM2:
			ucBuf[2] = '2';
			break;
		case AMA:
			ucBuf[2] = 'A';
			break;
	}	
	
#ifdef  ST7565P
	ST7565_UpdateSubDisplayData(&ucBuf[0]);
#endif
#ifdef  PT6524
	memcpy(&ucDISP_Buf[0], &ucBuf[0], 3);	
#endif
	
}

void TUNER_DISP_MainText(void)
{
#ifdef  MIC
	if	(MIC_GetMicStatus() == MIC_ON)
	{
		MIC_DISP_MainText();
		return;
	}
#endif
	if  (ucDISP_Blink == 1)
	{
		if  ((ucTuner_Mode == TUNER_SCAN) && (ucTuner_ModeSeq == TUNER_SCAN_WAIT_5SEC))
		{
			return;
		}
		if  (ucTuner_Mode == TUNER_AST)
		{
			switch  (ucTuner_ModeSeq)
			{
				case TUNER_AST_SETMUTE:
				case TUNER_AST_SET_SEEK:
				case TUNER_AST_FREQ_UP:
				case TUNER_AST_SET_FREQ:
				case TUNER_AST_GET_SIGNAL:
				case TUNER_AST_SET_PRESET:
				case TUNER_AST_REL_SEEK:
				case TUNER_AST_RELMUTE:
					break;
				case TUNER_AST_DEMO_RELMUTE:
				case TUNER_AST_DEMO_WAIT_5S:
				case TUNER_AST_DEMO_MUTE:
				case TUNER_AST_DEMO_FREQ:
				case TUNER_AST_END:
					return;
			}
		}
	}
	
	TUNER_DISP_Frequency();	

	if  (ucTuner_Mode == TUNER_SCAN)
	{
		if  (ucTuner_ModeSeq != TUNER_SCAN_WAIT_5SEC)
		{
			return;
		}
	}
	if  (ucTuner_Mode == TUNER_AST)
	{
		switch  (ucTuner_ModeSeq)
		{
			case TUNER_AST_SETMUTE:
			case TUNER_AST_SET_SEEK:
			case TUNER_AST_FREQ_UP:
			case TUNER_AST_SET_FREQ:
			case TUNER_AST_GET_SIGNAL:
			case TUNER_AST_SET_PRESET:
			case TUNER_AST_REL_SEEK:
				return;
			case TUNER_AST_DEMO_RELMUTE:
			case TUNER_AST_DEMO_WAIT_5S:
			case TUNER_AST_DEMO_MUTE:
			case TUNER_AST_DEMO_FREQ:
			case TUNER_AST_END:
			case TUNER_AST_RELMUTE:
				break;
		}
	}
	if  (ucTuner_Mode == TUNER_SEEK)
	{
		return;
	}
	TUNER_DISP_PresetNumber();
}

